package com.gxan.dao.impl;

import com.gxan.dao.TrainingDao;
import com.gxan.pojo.Training;
import com.gxan.utils.db.JdbcUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//培训管理Dao
public class TrainingDaoImpl extends JdbcUtils implements TrainingDao{
    //查询所有培训计划
    @Override
    public List<Training> finTrainingAll(Integer offset, Integer rows) throws SQLException {
        String sql="select * from training limit ?,?";
        Object[] params = {offset,rows};
        return super.queryListEntity(Training.class,sql,params);
    }
    //新增培训计划
    @Override
    public int addTraining(Training training) throws SQLException {
        String sql="insert into training(training_name,teacher,start_time,end_time,purpose,people,profile) values(?,?,?,?,?,?,?)";
        Object[] params = {training.getTraining_name(),training.getTeacher(),training.getStart_time(), training.getEnd_time(),training.getPurpose(),
                training.getPeople(),training.getProfile()};
        return super.executeUpdate(sql,params);
    }
    //查询总条数
    @Override
    public Integer countTraining() throws SQLException {
        String sql = "select count(*) from training";
        return super.findAllForInt(sql,null);
    }
    //修改培训计划
    @Override
    public int updateTraining(Training training) throws SQLException {
        String sql = "update training set training_name=?,teacher=?,start_time=?,end_time=?,purpose=?,people=?,profile=?,audit_status=2,training_url=? where training_id=?";
        Object[] params = {training.getTraining_name(),training.getTeacher(),training.getStart_time(),training.getEnd_time(),training.getPurpose(),
                           training.getPeople(),training.getProfile(),training.getTraining_url(),training.getTraining_id()};
        return super.executeUpdate(sql,params);
    }
    //根据id删除培训计划
    @Override
    public int deleteTrainingById(Integer training_id) throws SQLException {
        String sql = "delete from training where training_id=?";
        return super.executeUpdate(sql,training_id);
    }
    //按条件搜索
    @Override
    public List<Training> searchTraining(Integer offset, Integer rows, String training_name, String audit_status, String start_time, String end_time) throws SQLException {
        String sql = "select * from training where training_name like concat('%',?,'%')";
        List<Object> params = new ArrayList<>();
        params.add(training_name);
        if (audit_status !=null && !"".equals(audit_status)){
            sql = sql + "and audit_status = ?";
            params.add(audit_status);
        }
        if (start_time !=null && !"".equals(start_time)){
            sql = sql + "and start_time >= ?";
            params.add(start_time);
        }
        if (end_time !=null && !"".equals(end_time)){
            sql = sql + "and end_time <= ?";
            params.add(end_time);
        }
        sql = sql + "limit ?,?";
        params.add(offset);
        params.add(rows);
        return super.queryListEntity(Training.class,sql,params.toArray());
    }
    //查询搜索的总条数
    @Override
    public int countTrainingByParams(String training_name, String audit_status, String start_time, String end_time) throws SQLException {
        String sql = "select count(*) from training where training_name like concat('%',?,'%')";
        List<Object> params = new ArrayList<>();
        params.add(training_name);
        if (audit_status !=null && !"".equals(audit_status)){
            sql = sql + "and audit_status =?";
            params.add(audit_status);
        }
        if (start_time !=null && !"".equals(start_time)){
            sql = sql + "and start_time >=?";
            params.add(start_time);
        }
        if (end_time !=null && !"".equals(end_time)){
            sql = sql + "and end_time <=?";
            params.add(end_time);
        }
        return super.findAllForInt(sql,params.toArray());
    }
    //修改审核状态--通过审核
    @Override
    public int checkTraining(Training training) throws SQLException {
        String sql = "update training set training_name=?,teacher=?,start_time=?,end_time=?,purpose=?,people=?,profile=?,audit_status=3,audit_advice=?,training_url=? where training_id=?";
        Object[] params = {training.getTraining_name(),training.getTeacher(),training.getStart_time(),training.getEnd_time(),training.getPurpose(),
                training.getPeople(),training.getProfile(),training.getAudit_advice(),training.getTraining_url(),training.getTraining_id(),};
        return super.executeUpdate(sql,params);
    }
    //修改审核状态====驳回
    @Override
    public int rejectTraining(Training training) throws SQLException {
        String sql = "update training set training_name=?,teacher=?,start_time=?,end_time=?,purpose=?,people=?,profile=?,audit_status=1,audit_advice=?,training_url=? where training_id=?";
        Object[] params = {training.getTraining_name(),training.getTeacher(),training.getStart_time(),training.getEnd_time(),training.getPurpose(),
                training.getPeople(),training.getProfile(),training.getAudit_advice(),training.getTraining_url(),training.getTraining_id()};
        return super.executeUpdate(sql,params);
    }
    //添加培训计划与反馈
    @Override
    public int feedbackAddTraining(Training training) throws SQLException {
        String sql="update training set feedback=?,summary=?,result=? where training_id=?";
        Object[] params = {training.getFeedback(),training.getSummary(),training.getResult(),training.getTraining_id()};
        return super.executeUpdate(sql,params);
    }
}










